package com.example.hello1;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class ratelistactivitu extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private static final String Tag = "thred";
    ArrayList<HashMap<String, String>> lisItems;
    Myadapter my;
//    ListView list2;
    GridView list2;
    Handler handler;

    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ratelistactivitu);
        list2 = findViewById(R.id.list1018);

        SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY, "");
        Log.i("List","lastRateDateStr=" + logDate);


        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.i(Tag,"开始处理");
                if (msg.what == 6) {
                    ArrayList <RateItem> list3 = (ArrayList<RateItem>) msg.obj;
                    Log.i(Tag,"a+"+list3);
                    my= new Myadapter(ratelistactivitu.this, R.layout.list_rate, list3);
                    list2.setAdapter(my);
                    list2.setEmptyView(findViewById(R.id.no_data));
//                    list2.removeView();
                    list2.setOnItemClickListener(ratelistactivitu.this);//单击
                    list2.setOnItemLongClickListener(ratelistactivitu.this);//长按

                }
                super.handleMessage(msg);

            }
        };
        myadapter_thred_copy listthred = new myadapter_thred_copy(ratelistactivitu.this);
        listthred.setHadeler(handler);
        Thread t = new Thread(listthred);
        t.start();


    }
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object item=list2.getItemAtPosition(position);
        Log.i(Tag,"what");
        RateItem it= (RateItem) item;
//        String name=hashMap.get("ItemTitle");
//        String value=hashMap.get("ItemDetail");
        String name = it.getCname();
        String value = it.getCval();
        Intent intent=new Intent(ratelistactivitu.this,show_fetch_rate.class);
        intent.putExtra("ItemTitle",name);
        intent.putExtra("ItemDetail",value);
        Log.i(Tag,"ItemTitle"+name);
        Log.i(Tag,"ItemDetail"+value);
        startActivity(intent);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int pos, long l) {
        Log.i(Tag,"changan");
        AlertDialog.Builder builder = new AlertDialog.Builder(ratelistactivitu.this);
        builder.setTitle("notification").setMessage("Sure to delete?").
                setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       my.remove(list2.getItemAtPosition(pos));
                       my.notifyDataSetChanged();
                    }
                }).setNegativeButton("No",null);
        builder.create().show();
        Log.i(Tag,"long click to delete"+list2.getScrollBarSize());

        return true; //长按发生后不会相应单击，屏蔽单击事件
    }
}